{extend name="public/base"/}
{block name="body"}
<style type="text/css">
	.attrs{margin-top:5px;}
	.attrname{width:100px;border:solid 1px #ddd;height:31px;font-size:14px;padding-left:4px;}
	.attrval{width:40%;border:solid 1px #ddd;height:31px;font-size:14px;padding-left:4px;}
	.layui-upload-file{display:none;}
</style>
<title>添加客栈</title>
</head>
<body>
<article class="page-container">
	<form class="form form-horizontal" id="form-admin-add">
		<input type="hidden" class="input-text" value="{$data.id}" id="id" name="id">
		<div class="row cl">
			<label class="form-label col-xs-2 col-sm-2">介绍：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="text" class="input-text" value="{$data.des}" placeholder="例：悬崖池畔大床房" id="des" name="des">
			</div>
			<label class="form-label col-xs-2 col-sm-2">床：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="text" class="input-text" value="{$data.bed}" placeholder="例：2.0*1.8" id="bed" name="bed">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2 col-sm-2">楼层：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="text" class="input-text" value="{$data.storey}" placeholder="例：1楼" id="storey" name="storey">
			</div>
			<label class="form-label col-xs-2 col-sm-2">面积：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="text" class="input-text" value="{$data.area}" placeholder="例：60㎡" id="area" name="area">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2 col-sm-2">早餐：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="text" class="input-text" value="{$data.breakfast}" placeholder="例：双早" id="breakfast" name="breakfast">
			</div>
			<label class="form-label col-xs-2 col-sm-2">可住人数：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="number" class="input-text" value="{$data.peoples}" placeholder="例：1" id="peoples" name="peoples">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2 col-sm-2">网络：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="text" class="input-text" value="{$data.wifi}" placeholder="例：WiFi" id="wifi" name="wifi">
			</div>
			<label class="form-label col-xs-2 col-sm-2">价格：</label>
			<div class="formControls col-xs-4 col-sm-4">
				<input type="number" class="input-text" min="1" value="{$data.price}" placeholder="例：999" id="price" name="price" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-2 col-sm-2">是否无烟房：</label>
			<div class="formControls col-xs-4 col-sm-4">
               <label class="radio-inline">
                   <input type="radio" id="smoking1" name="non_smoking" value="1"  {if $data['non_smoking']==1}checked{/if} />
                   <label for="smoking1">是</label>
               </label>
               <label class="radio-inline">
                   <input type="radio" id="smoking2" name="non_smoking" value="2"  {if $data['non_smoking']==2}checked{/if}/>
                   <label for="smoking2">否</label>
               </label>
			</div>
			<label class="form-label col-xs-2 col-sm-2">是否可加床：</label>
			<div class="formControls col-xs-4 col-sm-4">
               <label class="radio-inline">
                   <input type="radio" id="more_bed1" name="more_bed" value="1"  {if $data['more_bed']==1}checked{/if} />
                   <label for="more_bed1">是</label>
               </label>
               <label class="radio-inline">
                   <input type="radio" id="more_bed2" name="more_bed" value="2"  {if $data['more_bed']==2}checked{/if} />
                   <label for="more_bed2">否</label>
               </label>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">多图：</label>
			<div class="formControls col-xs-8 col-sm-9">
				
				<input type="hidden" name="imgs" class="multiple_show_img" value=""> 
				<div class="layui-upload">
				        <button type="button" class="layui-btn" id="multiple_img_upload">多图片上传</button>
				        <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
				            <div class="layui-upload-list" id="div-slide_show">
            					{foreach $data.imgsarr as $value }
				            	<img src="{$value}"  title="点击删除" class="layui-upload-img" style="width: 100px;height: 100px;" onclick="delMultipleImgs(this)">
				            	{/foreach}
				            </div>
				        </blockquote>
				        <div style="font-size:10px;color:#999999;">(建议尺寸：300*160)</div>
				</div>

			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">排序：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="number" class="input-text" value="{$data.sort}" placeholder="0" id="sort" name="sort">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-3 col-sm-2">是否启用：</label>
			<div class="formControls col-xs-8 col-sm-9">
               <label class="radio-inline">
                   <input type="radio" id="emailwy1" name="state" value="1" {if $data['state']==1}checked{/if} />
                   <label for="emailwy1">是</label>
               </label>
               <label class="radio-inline">
                   <input type="radio" id="emailwy2" name="state" value="2" {if $data['state']==2}checked{/if} />
                   <label for="emailwy2">否</label>
               </label>
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<input id="submit" class="btn btn-primary radius" type="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</div>
	</form>
</article>

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script src="/static/lib/layui-v2.4.5/layui-v2.4.5/layui/layui.js"></script>
<script type="text/javascript">

	$(function(){
		$("#more_bed1").on('click',function(){
			$("#more_bed2").attr('checked', false);
			$(this).attr('checked', true);
		});
		$("#more_bed2").on('click',function(){
			$("#more_bed1").attr('checked', false);
			$(this).attr('checked', true);
		});
		$("#smoking1").on('click',function(){
			$("#smoking2").attr('checked', false);
			$(this).attr('checked', true);
		});
		$("#smoking2").on('click',function(){
			$("#smoking1").attr('checked', false);
			$(this).attr('checked', true);
		});
		$("#emailwy1").on('click',function(){
			$("#emailwy2").attr('checked', false);
			$(this).attr('checked', true);
		});
		$("#emailwy2").on('click',function(){
			$("#emailwy1").attr('checked', false);
			$(this).attr('checked', true);
		});
	});
	var multiple_images = [];
	if("{$data.imgs}"){
		multiple_images = "{$data.imgs}".split(",");
        $('.multiple_show_img').val(multiple_images);
	}

    //单击图片删除图片 【注册全局函数】
    function delMultipleImgs(this_img){
    	console.log(this_img)
        //获取下标
        var subscript=$("#div-slide_show img").index(this_img);
    	console.log(subscript)
        //删除图片
        this_img.remove();
    	console.log(this_img)
        //删除数组
    	console.log(multiple_images)
        multiple_images.splice(subscript, 1);
    	console.log(multiple_images)
        //重新排序
        multiple_images.sort();
    	console.log(multiple_images)
        $('.multiple_show_img').val(multiple_images);
        //返回
        return ;
    }
    layui.use([ 'upload'], function () {
       
        var upload = layui.upload;
        //多图片上传
        upload.render({
            elem: '#multiple_img_upload'
            ,url: "{:url('uploadtmd')}"
            ,multiple: true
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#div-slide_show').append('<img src="'+ result +'" alt="'+ file.name +'" title="点击删除" class="layui-upload-img" style="width: 100px;height: 100px;" onclick="delMultipleImgs(this)">&nbsp;')
                });
            }
            ,done: function(res){
                //如果上传成功
                if (res.code == 200) {
                    //追加图片成功追加文件名至图片容器
                    multiple_images.push(res.photo);
                    $('.multiple_show_img').val(multiple_images);
                }else {
                    //提示信息
                   layer.msg(res.msg);
                }
            }
        });

    });
$("#submit").click(function() {
	layer.load(1, {
	  shade: [0.1,'#fff'] //0.1透明度的白色背景
	});
	var id=$("input[name=id]").val();
	var des=$("input[name=des]").val();
	var bed=$("input[name=bed]").val();
	var storey=$("input[name=storey]").val();
	var breakfast=$("input[name=breakfast]").val();
	var peoples=$("input[name=peoples]").val();
	var wifi=$("input[name=wifi]").val();
	var area=$("input[name=area]").val();
	var more_bed=$("input[name=more_bed][checked]").val();
	var non_smoking=$("input[name=non_smoking][checked]").val();
	var state=$("input[name=state][checked]").val();
	var imgs=$("input[name=imgs]").val();
	var price=$("input[name=price]").val();
	var sort=$("input[name=sort]").val();
	$.ajax({
	    type: 'POST',
	    url: "{:url('homestay_edit')}",
	    data: {id:id,des:des,bed:bed,storey:storey,breakfast:breakfast,peoples:peoples,area:area,wifi:wifi,more_bed:more_bed,non_smoking:non_smoking,imgs:imgs,sort:sort,state:state,price:price},
	    success: function(resdata){
	        if(resdata.code==200){
				layer.msg(resdata.msg,{icon:1,time:1000});
				setTimeout("parent.location.reload()",1000);
	        }else{
				layer.msg(resdata.msg,{icon:2,time:3000});
	        }
	    },
	    error: function(){
			layer.msg('数据保存失败',{icon:2,time:3000});
	    }
	})
})
</script> 
{/block}